//
//  UITableViewCell+SectionCircular.m
//  BTShike
//
//  Created by 刘军林 on 2019/5/24.
//  Copyright © 2019 刘军林. All rights reserved.
//

#import "UITableViewCell+SectionCircular.h"

#define CornerRadiusValue 7.5
#define LeftRightSpace 10

@implementation UITableViewCell (SectionCircular)

-(void) setSectionCircularTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath;
{
    self.backgroundColor = [UIColor clearColor];

    NSInteger topSpace = 0;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(LeftRightSpace, topSpace, tableView.frame.size.width - LeftRightSpace - LeftRightSpace, self.frame.size.height)];
    bgView.backgroundColor = [UIColor clearColor];
    [self.contentView insertSubview:bgView atIndex:0];
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = bgView.bounds;
    bgView.layer.mask = maskLayer;
    maskLayer.fillColor = [UIColor whiteColor].CGColor;
    if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
    {
        path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds cornerRadius:CornerRadiusValue];
    }
    else if (indexPath.row == 0)
    {
        path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectEdgeLeft|UIRectEdgeTop cornerRadii:CGSizeMake(CornerRadiusValue, CornerRadiusValue)];
    }
    else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
    {
        path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectEdgeRight|UIRectEdgeBottom cornerRadii:CGSizeMake(CornerRadiusValue, CornerRadiusValue)];
    }
    else
    {
        path = [UIBezierPath bezierPathWithRect:bgView.bounds];
    }
    maskLayer.path = path.CGPath;
    [bgView.layer addSublayer:maskLayer];
    [[self.contentView viewWithTag:9999] removeFromSuperview];
    if (indexPath.row != [tableView numberOfRowsInSection:indexPath.section]-1  )
    {
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(LeftRightSpace+7, self.frame.size.height - 1, self.frame.size.width - LeftRightSpace * 2-14, 0.3)];
        line.backgroundColor = tableView.separatorColor;
        line.tag = 9999;
        [self.contentView addSubview:line];
    }
}


@end
